package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
/**
 * @author hql
 * @date 2019-12-02 11:47
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

   @Reference
    private CheckGroupService checkGroupService;

    /**
     * 查询所有检查项的信息
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        boolean flag=false;
        String message=null;

        List<CheckItem> checkItems = null;
        try {
            checkItems = checkGroupService.findAll();
            flag=true;
            message= MessageConstant.QUERY_CHECKITEM_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
        }

        return new Result(flag,message,checkItems);
    }

    /**
     * 添加检查组并且附带检查组中含有的检查项信息
     * @param id
     * @param checkGroup
     * @return
     */
    @RequestMapping("/addCheck")
    public Result addCheck(Integer[] id, @RequestBody CheckGroup checkGroup){
        boolean flag=false;
        String message=null;
        try {
            checkGroupService.addCheck(id,checkGroup);
            flag=true;
            message= MessageConstant.QUERY_CHECKITEM_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
        }

        return new Result(flag,message);
    }

    /**
     * 分页查询检查项
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findCheckGroup")
    public Result findCheckGroup(@RequestBody QueryPageBean queryPageBean){
        boolean flag=false;
        String message=null;
        PageResult pageResult=null;
        try {
         pageResult=checkGroupService.findCheckGroup(queryPageBean);
            flag=true;
            message= MessageConstant.QUERY_CHECKITEM_SUCCESS;
        }
        catch (RuntimeException r){
            message=r.getMessage();
        }catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
        }

        return new Result(flag,message,pageResult);
    }

    /**
     * 根据id回显检查组信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        boolean flag=false;
        String message=null;
        CheckGroup checkGroup=null;

        try {
          checkGroup = checkGroupService.findById(id);
            flag=true;
            message= MessageConstant.QUERY_CHECKITEM_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
        }

        return new Result(flag,message,checkGroup);
    }

    /**
     * 根据检查组的id获取对应的检查项的id
     * @param id
     * @return
     */

    @RequestMapping("/findCheckItemById")
    public Result findCheckItemById(Integer id){
        boolean flag=false;
        String message=null;
        Integer[] ids=null;

        try {
            ids = checkGroupService.findCheckItemById(id);
            flag=true;
            message= "回显检查项信息成功";
        } catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
        }

        return new Result(flag,message,ids);
    }

    /**
     * 编辑检查组的信息
     * @param id
     * @param checkGroup
     * @return
     */
        //updateCheckGroup
        @RequestMapping("/updateCheckGroup")
        public Result updateCheckGroup(Integer[] id, @RequestBody CheckGroup checkGroup){
            boolean flag=false;
            String message=null;
            try {
                checkGroupService.updateCheckGroup(id,checkGroup);
                flag=true;
                message= MessageConstant.QUERY_CHECKITEM_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                message=MessageConstant.QUERY_CHECKITEM_FAIL;
            }

            return new Result(flag,message);
        }

    /**
     * 根据id删除对应的检查组
     * @param id
     * @return
     */

    @RequestMapping("/delCheckGroup")
    public Result delCheckGroup(Integer id){
        boolean flag=false;
        String message=null;
        try {
             checkGroupService.delCheckGroup(id);
            flag=true;
            message= "回显检查项信息成功";
        }catch (RuntimeException e){
            message=e.getMessage();
        }catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
        }

        return new Result(flag,message);
    }


    /**
     * 查询所有的检查组信息
     * @return
     */
    @RequestMapping("/findAllCheckGroup")
    public Result findAllCheckGroup(){
        boolean flag=false;
        String message=null;
        List<CheckGroup> checkGroupList=null;
        try {
            checkGroupList=checkGroupService.findAllCheckGroup();
            flag=true;
            message= MessageConstant.QUERY_CHECKITEM_SUCCESS;
        }
        catch (RuntimeException r){
            message=r.getMessage();
        }catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
        }

        return new Result(flag,message, checkGroupList);
    }
}
